package com.maven.qqspider.util;

import java.awt.Image;
import java.util.HashMap;

import javax.swing.ImageIcon;

import com.maven.qqspider.common.Constant;
import com.maven.qqspider.util.ResourceLoader;

public class ImageManager {

	private static HashMap<String, ImageIcon> imageRegistry = new HashMap<String, ImageIcon>();

	public static HashMap<String, ImageIcon> getImageRegistry() {
		if (imageRegistry == null) {
			imageRegistry = new HashMap<String, ImageIcon>();
		}
		return imageRegistry;
	}

	public static ImageIcon getImageIcon(String imageName) {
		return getImageIcon(imageName, null);
	}

	public static ImageIcon getImageIcon(String imageName, String description) {
		ImageIcon getImageIcon = imageRegistry.get(imageName);
		if (getImageIcon == null) {
			getImageIcon = description == null ? new ImageIcon(ResourceLoader
					.getResouce(imageName)) : new ImageIcon(ResourceLoader
					.getResouce(imageName), description);
			imageRegistry.put(imageName, getImageIcon);
		}
		return getImageIcon;
	}

	public static Image getImage(String imageName) {
		return getImageIcon(imageName).getImage();
	}
	
	/**
	 * use "resource/image" as the default directory
	 * 
	 * @param imageName
	 * 
	 * @return ImageIcon
	 * */
	public static ImageIcon getImageIconByShortName(String imageName) {
		return getImageIcon(Constant.ICON_DIR + Constant.getSlash() + imageName);
	}
	
	/**
	 * use "resource/image" as the default directory
	 * 
	 * @param imageName
	 * 
	 * @return Image
	 * */
	public static Image getImageByShortName(String imageName) {
		return getImageIcon(Constant.ICON_DIR + Constant.getSlash() + imageName)
				.getImage();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
